package cn.wolfcode.mq;

import cn.wolfcode.config.WebSocketServer;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;

@Slf4j
@Component
@RocketMQMessageListener(
        consumerGroup = "MQOrderResultListener",
        topic = MQConstant.ORDER_RESULT_TOPIC,
        selectorExpression = MQConstant.ORDER_RESULT_SUCCESS_TAG + "||" + MQConstant.ORDER_RESULT_FAIL_TAG
)
public class MQOrderResultListener implements RocketMQListener<OrderMQResult> {
    @Override
    public void onMessage(OrderMQResult message) {
        log.info("进入到通知的队列中");
        try {
            Thread.sleep(1000);
        } catch (Exception e) {
            e.printStackTrace();
        }
        WebSocketServer webSocketServer = WebSocketServer.clients.get(message.getToken());
        if (webSocketServer != null) {
            try {
                webSocketServer.getSession().getBasicRemote().sendText(JSON.toJSONString(message));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
